var mat = new vo.newObj({
	parent:'#mat',
	el:'#mat',
	datas:{
		check:0,
		upload:'上传素材',
		del:'删除素材(0)',
		select:'确认选择(0)',
		editor:'编辑',
		exit:'退出编辑',
		inner:{
			selectDiv:getElementsByAttr_r(document, "class", "select")[0],
			editDiv:getElementsByAttr_r(document, "class", "edit")[0],
			selectTag:getElementsByAttr_r(document,"name","select")[0],
			delTag:getElementsByAttr_r(document,"name","del")[0]
		},
		wind:getElementsByAttr_r(document, "class", "wind")[0],
		warn:getElementsByAttr_r(document, "class", "warn")[0]
	},
	methods:{
		close:function(datas){
			this.el.style.display = "none";
			datas.inner.editDiv.style.display="none";
			datas.inner.selectDiv.style.display="block";
			this.methods.init();
		},
		selectUpload:function(){
			var upload = getElementsByAttr_r(this.el, "name", "pic")[0];
			upload.click();
		},
		select:function(){
			var imgs = "";
			var pattern = "![博主素材](#{url})";
			var selecteds = getElementsByAttr_r(this.el, "class", "selected");
			for(var i = 0 ;i<selecteds.length;i++){
				var src = selecteds[i].getAttribute("src");
				imgs+=(pattern.replace("#{url}",src)+"\n");
			}
			editor.modify("md",editor.get("md")+imgs);
			editor.methods.md(getElementsByAttr_r(editor.el,"name","md")[0]);
			mat.methods.close();
		},
		toggle:function(datas){
			var t = target();
			if(t.tagName == "IMG"){
				if(null == t.getAttribute("class")){
					t.setAttribute("class","selected");
					this.modify("check",datas.check+1);
				}else{
					t.removeAttribute("class");
					this.modify("check",datas.check-1);
				}
				var select = datas.inner.selectDiv;
				var edit = datas.inner.editDiv;
				if(select.style.display == "block"){
					if(datas.check > 0){
						datas.inner.selectTag.setAttribute("class","active");
					}else{
						datas.inner.selectTag.setAttribute("class","disabled");
					}
					this.modify("select",datas.select.replace(/\w+/,datas.check));
				}else if(edit.style.display == "block"){
					if(datas.check > 0){
						datas.inner.delTag.setAttribute("class","error");
					}else{
						datas.inner.delTag.setAttribute("class","disabled");
					}
					this.modify("del",datas.del.replace(/\w+/,datas.check));
				}
			}
		},
		editor:function(datas){
			datas.inner.editDiv.style.display="block";
			datas.inner.selectDiv.style.display="none";
			this.methods.init();
		},
		exit_editor:function(datas){
			datas.inner.editDiv.style.display="none";
			datas.inner.selectDiv.style.display="block";
			this.methods.init();
		},
		init:function(datas){
			this.modify("del","删除素材(0)");
			this.modify("select","确认选择(0)");
			this.modify("check",0);
			datas.inner.selectTag.setAttribute("class","disabled");
			datas.inner.delTag.setAttribute("class","disabled");
			var imgs = this.el.getElementsByTagName("img");
			for(var i = 0;i<imgs.length;i++){
				imgs[i].removeAttribute("class");
			}
		},
		del:function(datas){
			var t = target();
			if(t.getAttribute("class") == "disabled"){
				return;
			}
			var selecteds = getElementsByAttr_r(this.el, "class", "selected");
			var paths = [];
			var prefix = basepath+"admin/mat";
			for(var i = 0 ;i<selecteds.length;i++){
				var src = selecteds[i].getAttribute("src");
				src = src.substring(prefix.length);
				paths.push(src);
			}
			this.methods.init();
			var wind = datas.wind;
			var warn = datas.warn;
			ajax({
				method:'PUT',
				url:'admin/mat/',
				datas:paths,
				success:function(resp){
					if(resp.status == "200"){
						warn.innerHTML = "删除素材成功";
						warn.setAttribute("class","warn ok");
						mat.methods.load();
						timeout(wind, warn);
					}else{
						warn.innerHTML = resp.status+":"+resp.datas;
						warn.setAttribute("class","warn error");
						timeout(wind, warn);
					}
					wind.style.display = "inline-block";
				},
				error:function(){
					warn.innerHTML = "删除素材失败";
					warn.setAttribute("class","warn error");
					timeout(wind, warn);
					wind.style.display = "inline-block";
				}
			});
		},
		upload:function(datas){
			var formData = new FormData(target().parentNode);//只支持H5，没办法，这是新时代了！
			var t = getElementsByAttr_r(this.el, "name", "upload")[0];
			var wind = datas.wind;
			var warn = datas.warn;
			t.setAttribute("disabled","disabled");
			t.innerHTML ="正在上传";
			ajax({
				method:"POST",
				dataType:'FILE',
				url:'admin/mat/',
				datas:formData,
				success:function(resp){
					console.log(resp.status);
					if(resp.status == "200"){
						warn.innerHTML = "上传素材成功";
						warn.setAttribute("class","warn ok");
						timeout(wind, warn);
						mat.methods.load();
					}else{
						warn.innerHTML = resp.status+":"+resp.datas;
						warn.setAttribute("class","warn error");
						timeout(wind, warn);
					}
					t.setAttribute("disabled","false");
					t.innerHTML = "上传素材";
					wind.style.display = "inline-block";
				},
				error:function(){
					warn.innerHTML = "上传素材失败";
					warn.setAttribute("class","warn error");
					timeout(wind, warn);
					t.setAttribute("disabled","false");
					t.innerHTML = "上传素材";
					wind.style.display = "inline-block";
				}
			});
		},
		load:function(datas){
			ajax({
				method:'GET',
				url:'admin/mat/list?'+Math.random(),
				success:function(resp){
					var datas = resp.datas;
					var content = getElementsByAttr_r(mat.el, "class", "mat-content")[0];
					content.innerHTML = "";
					for(var i = 0;i<datas.length;i++){
						content.innerHTML+="<img vo-m='click:toggle' src='"+basepath+"admin/mat"+datas[i].path+"'/>";
					}
				},
				error:function(){
					throw new Error("加载素材失败");
				}
			});
		}
	}
});